package cn.wuxun.elasticsearch.config;

import java.util.List;
import java.net.InetSocketAddress;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ESConfig {
	// private List<String> nodes;
	private String nodes;

	// 初始化方法
	// 初始化方法名唯一
	@Bean // 自定义工程依赖的配置工程中
	public TransportClient initESTransportClient() {
		TransportClient client = new PreBuiltTransportClient(Settings.EMPTY);
		// nodes解析出来3个节点的host和port
		// "host:port"
		// for (String node : nodes) {
		String host = nodes.split(":")[0];
		Integer port = Integer.parseInt(nodes.split(":")[1]);
		client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(host, port)));
		// client.addTransportAddress(new InetSocketTransportAddress(new
		// InetSocketAddress("10.42.115.99", 9300)));
		// }
		return client;
	}

	//
	public String getNodes() {
		return nodes;
	}

	public void setNodes(String nodes) {
		this.nodes= nodes;
	}

	// public List<String> getNodes() {
	// return nodes;
	// }
	// public void setNodes(List<String> nodes) {
	// this.nodes = nodes;
	// }

}
